﻿using System;
using System.Threading.Tasks;
using JT808.Gateway.Abstractions.Extensions;
using JT808.Gateway.Extensions;
using JT808.Gateway.QueueService.Extensions;
using JT808.Gateway.RabbitMQ;
using JT808.Gateway.SimpleQueueService.Impl;
using JT808.Protocol;
using JT808.Protocol.Extensions.JT1078;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;

namespace JT808.Gateway.QueueService
{
    class Program
    {
        static async Task Main(string[] args)
        {

            var hostBuilder = new HostBuilder()
                .ConfigureAppConfiguration((hostContext, config) =>
                {
                    config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
                    config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                })
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddJT808Configure()
                        .AddJT1078Configure()
                        .AddClientBuilder()
                        .AddRabbitMsgConsumer(hostContext.Configuration)
                        .AddRabbitNotifyProducer(hostContext.Configuration)
                        .AddRabbitMsgReplyProducer(hostContext.Configuration)
                        .AddRabbitSessionConsumer(hostContext.Configuration)
                        .AddReplyMessage<JT808QueueReplyMessageHandlerImpl>()
                        .AddSessionNotice<JT808SessionNoticeServiceImpl>()
                        .AddDbContext<JT808DbContext>(hostContext.Configuration);
                })
                .UseSerilog((context, loggerConfiguration) =>
                {
                    loggerConfiguration.ReadFrom.Configuration(context.Configuration);
                });

            await hostBuilder.RunConsoleAsync();
        }
    }
}
